package me.android.library.utils.android;

import android.net.Uri;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import lombok.experimental.UtilityClass;
import mylab.utils.common.IoUtil;

@UtilityClass
public class AssetUtil {
    public Uri asset2Uri(String fileName) {
        return Uri.fromFile(new File("file:///android_asset/" + fileName));
    }

    /**
     * 从assets读取字符串
     */
    public String asset2String(String fileName) {
        byte[] bytes = asset2Bytes(fileName);
        if (bytes != null) {
            return new String(bytes, StandardCharsets.UTF_8);
        }

        return null;
    }

    /**
     * 从assets读取字节流
     */
    public byte[] asset2Bytes(String fileName) {
        try (InputStream stream = SystemServiceUtil.getAssetManager().open(fileName)) {
            return IoUtil.readBytes(stream);
        } catch (IOException e) {
            return null;
        }
    }

}
